﻿using Newtonsoft.Json;
using ProductionManage.Models.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using wangyaxin.Jiangzhenying.UnitInfos;
using wangyaxin.Jiangzhenying.UnitInfos.Dto;
using wangyaxin.Jiangzhenying.WorkstationWorkers.Dto;
using wangyaxin.Models;
using wangyaxin.Wang.Dto;
using static Volo.Abp.Identity.Settings.IdentitySettingNames;
using Microsoft.AspNetCore.Mvc;
using System.Runtime;

namespace wangyaxin.Jiangzhenying
{
    [ApiExplorerSettings(GroupName = "江珍莹")]
    public class UnitInfoServise : ApplicationService, IUnitInfoIService
    {
        private readonly IRepository<UnitInfo, int> _repository;
        private readonly IRepository<WangUser, int> _WangUserRepository;

        public UnitInfoServise(IRepository<UnitInfo, int> repository, IRepository<WangUser, int> WangUserRepository)
        {
            _repository = repository;
            _WangUserRepository = WangUserRepository;
        }

        /// <summary>
        /// 显示单位信息
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetUnidInfoList(string? unitCode, string? unitName)
        {
            var list = await _repository.GetListAsync();
            if (!string.IsNullOrEmpty(unitCode))
            {
                list = list.Where(x => x.UnitCode.Contains(unitCode)).ToList();
            }
            if (!string.IsNullOrEmpty(unitName))
            {
                list = list.Where(x => x.UnitName.Contains(unitName)).ToList();
            }
            var UnitInfoList = ObjectMapper.Map<List<UnitInfo>, List<UnitInfoDto>>(list);
            return JsonConvert.SerializeObject(new PageDto<UnitInfoDto> { code = "", data = UnitInfoList, msg = "", count = UnitInfoList.Count });
        }

        /// <summary>
        /// 新增单位信息
        /// </summary>
        /// <returns></returns>
        public async Task<UnitInfo> AddUnitInfo(UnitInfo unitInfo)
        {
            var addUnitInfo=ObjectMapper.Map<UnitInfo, UnitInfo>(unitInfo);
            await _repository.InsertAsync(addUnitInfo);
            return addUnitInfo;
        }

        /// <summary>
        /// 根据id获取单位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet, Route("/Jiang/GetUnitInfo")]
        public async Task<UnitInfo> GetUnitInfo(int id)
        {
            var unitInfo=await _repository.GetAsync(id);
            return unitInfo;
        }

        /// <summary>
        /// 更新单位信息
        /// </summary>
        /// <param name="unitInfo"></param>
        /// <returns></returns>
        [HttpGet, Route("/Jiang/UpdateUnitInfo")]
        public async Task<int> UpdateUnitInfo(UpdateUnitInfoDto unitInfo)
        {
            var updUnitInfo = await _repository.GetAsync(x => x.Id == unitInfo.Id);
            updUnitInfo.UnitCode = unitInfo.UnitCode;
            updUnitInfo.UnitName = unitInfo.UnitName;
            updUnitInfo.UnitFlag = unitInfo.UnitFlag;
            updUnitInfo.PrimaryId = unitInfo.PrimaryId;
            updUnitInfo.ChangRate = unitInfo.ChangRate;
            updUnitInfo.IsEnable = unitInfo.IsEnable;
            updUnitInfo.Remark = unitInfo.Remark;
            await _repository.UpdateAsync(updUnitInfo);
            return 1;
        }

        /// <summary>
        /// 删除单位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<string> DeleteUnitInfo(int id)
        {
            var delUnitInfo = await _repository.GetAsync(x=>x.Id==id);
            await _repository.DeleteAsync(delUnitInfo,true);
            return JsonConvert.SerializeObject(new PageDto<UnitInfoDto> { code = "200", data = null, msg = "成功", count = 1 });
        }
    }
}
